package com.yocard.yxyd.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.adapter.CsmRefundAdapter;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.TrackListBean;
import com.yocard.yxyd.ui.activity.AddFollwActivity;
import com.yocard.yxyd.ui.contract.CsmRefundContract;
import com.yocard.yxyd.ui.fragment.base.BaseFragment;
import com.yocard.yxyd.ui.presenter.RefundPresenter;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.yocard.yxyd.event.EventCode.ORDER_REUND_SUCCESS;

/**
 * Created on 2019/4/16.
 * 跟进记录 05/07
 */
public class CsmReundListFragment extends BaseFragment<RefundPresenter> implements CsmRefundContract.View {

    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;

    List<TrackListBean.OrderTrackEntity> listBeans = new ArrayList<>();
    protected CsmRefundAdapter mAdapter;
    protected String csmId;
    @BindView(R.id.btn_add_handle_info)
    TextView btnAddHandleInfo;
    boolean orderReunded;//订单是否已成功退单

    @Override
    protected int getLayoutId() {
        return R.layout.ft_csm_refund_list;
    }

    @Override
    protected void initEventAndData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            csmId = bundle.getString("csmId");
            showLoadingDialog();
            mPresenter.fetchList(csmId);
        }
        mAdapter = new CsmRefundAdapter(listBeans, csmId);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void onFetchList(TrackListBean Beans) {
        dismisDialog();
        listBeans.clear();
        if (Beans.getOrder_track() != null) {
            listBeans.addAll(Beans.getOrder_track());
        }
        mAdapter.setCanTrack(Beans.getCan_track());
        mAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFreshEvent(CommonBean bean) {
        if (bean.getCode() == Constants.CODE_REFRESH) {
            showLoadingDialog();
            mPresenter.fetchList(csmId);
        } else if (bean.getCode() == ORDER_REUND_SUCCESS) {
            orderReunded = true;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.btn_add_handle_info)
    public void onViewClicked() {
        if(orderReunded) {
            ToastUtils.showShort(R.string.order_refunded);
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("csmId", csmId);
        ActivityUtils.startActivity(bundle, AddFollwActivity.class);
    }

}
